/*
OPTIMAL BANDWIDTH EVENT GRAPHS

Data 	: 
Folder 	: 
Date	: 2020-01-29

Creator		: Jonas Cederlof	(JC)
Description : 
Notes:


LATEST UPDATE: 	

*/

********************************************************************************

clear
set more		 off
cap   log close 	_all

log using 	"../log/B4_Obandwidth_graph.log"	, replace 
use 		"$datapath/A3_age55data_WC_rdrobust.dta"




*=============
xtset   ym_def
*=============

{ // Define globals
*===============================================================================
global covariets 	"C_annual_ear_prenot_def C_female C_immigrant C_tenureatnot C_educ1 C_educ2 C_educ3 "		
global instrument	"treat"
global linear		"runvar inter"
global quadratic	"$linear q_runvar q_inter"
global cubic 		"$quadratic c_runvar c_inter"
}
*




*Keep relevant eventtime
keep if inrange(eventtime_def,-12,48)


	

{ // Pr(Other firm)
*===============================================================================
*Generate: indicator for working at other firm than the notifying one
*rename otherfirm2 otherfirm_prio2
*gen otherfirm2 = working2==1 & atfirm2==0


matrix B = J(61,5,.)
local i = 0

forvalues t = 0(1)48{
	local i = `i' + 1
	display `t'
	qui rdbwselect otherfirm2 runvar  if eventtime_def==`t' , vce(cluster varselid)
	matrix B[`i',5] = round(e(h_mserd),0.1)
	local b = e(h_mserd)
	qui xtreg otherfirm2	$instrument $linear $covariets 	if eventtime_def==`t' & inrange(runvar,-`b',`b')  , cluster(varselid) fe nonest 

	matrix B[`i',1]  = `t'
	matrix B[`i',2]  = _b[_cons]
	matrix B[`i',3]  = _b[_cons] + _b[treat]
	matrix B[`i',4]  = abs(_b[treat]/_se[treat]) >=1.96
}
svmat B
rename B1 t
rename B2 C
rename B3 T
rename B4 sig
rename B5 bw

gen number = _n if bw!=.
gen odd = mod(number,2) 
gen bw2 = bw if odd==0
gen bw3 = bw if odd==1


tostring bw2, gen(s_bw2) force
gen subs_bw2 = substr(s_bw2,1,3)
replace subs_bw2 = "" if subs_bw2=="."



tostring bw3, gen(s_bw3) force
gen subs_bw3 = substr(s_bw3,1,3)
replace subs_bw3 = "" if subs_bw3=="."

*Graph
local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
local Sstyle "lpattern(shortdash_dot) lcolor(gray) vertical" 


twoway 	(connected T t, `Tstyle' mlabel(subs_bw3) mlabcolor(black) mlabgap(2) mlabposition(6) mlabsize(vsmall) ) ///
	(connected C t, `Cstyle' mlabel(subs_bw2) mlabcolor(black) mlabgap(2) mlabposition(12) mlabsize(vsmall)) (rspike T C t if sig==1, `Sstyle') ///
,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(-12 48)) xlabel(-12(6)48)  ///
xtitle("Months after notification") ytitle("Pr(Other firm)") name(a,replace)  ///
graphregion(color(white)) legend(order( 1 "Long notification" 2 "Short notification" ) region(color(white)))
graph export "$agegraphpath/Robust/event_RD_otherfirm_BWrobust.pdf",replace

drop t C T sig bw* s_bw* subs_* number odd
}
*
{ // Pr(At firm)
*===============================================================================
matrix B = J(61,5,.)
local i = 0

forvalues t = -12(1)48{
	local i = `i' + 1
	display `t'
	qui rdbwselect atfirm2 runvar  if eventtime_def==`t' , vce(cluster varselid)
	matrix B[`i',5] = round(e(h_mserd),0.1)
	local b = e(h_mserd)
	qui xtreg atfirm2	$instrument $linear $covariets 	if eventtime_def==`t' & inrange(runvar,-`b',`b')  , cluster(varselid) fe nonest 

	matrix B[`i',1]  = `t'
	matrix B[`i',2]  = _b[_cons]
	matrix B[`i',3]  = _b[_cons] + _b[treat]
	matrix B[`i',4]  = abs(_b[treat]/_se[treat]) >=1.96
}
svmat B
rename B1 t
rename B2 C
rename B3 T
rename B4 sig
rename B5 bw

gen number = _n if bw!=.
gen odd = mod(number,2) 
gen bw2 = bw if odd==0
gen bw3 = bw if odd==1


tostring bw2, gen(s_bw2) force
gen subs_bw2 = substr(s_bw2,1,3)
replace subs_bw2 = "" if subs_bw2=="."



tostring bw3, gen(s_bw3) force
gen subs_bw3 = substr(s_bw3,1,3)
replace subs_bw3 = "" if subs_bw3=="."

*Graph
local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
local Sstyle "lpattern(shortdash_dot) lcolor(gray) vertical" 


twoway 	(connected T t, `Tstyle' mlabel(subs_bw3) mlabcolor(black) mlabgap(2) mlabposition(12) mlabsize(vsmall) ) ///
	(connected C t, `Cstyle' mlabel(subs_bw2) mlabcolor(black) mlabgap(2) mlabposition(6) mlabsize(vsmall)) (rspike T C t if sig==1, `Sstyle') ///
,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(-12 48)) xlabel(-12(6)48)  ///
xtitle("Months relative to notification") ytitle("Pr(at notifying firm)") name(a,replace)  ///
graphregion(color(white)) legend(order( 1 "Above age 55" 2 "Below age 55" ) region(color(white)))
graph export "$agegraphpath/Robust/event_RD_atfirm_BWrobust.pdf",replace

drop t C T sig bw* s_bw* subs_* number odd
}
*


{ // Annual earnings (t+1) for different bandwidths
*===============================================================================
preserve
	keep if eventtime_def==12
	local outcome "annual_ear"
	replace `outcome' = `outcome'/1000
	*==========
	xtset   ym_def
	*==========
	
	 rdbwselect `outcome' runvar   , vce(cluster varselid)
	local opb = round(e(h_mserd),.01)
	
	
	foreach b of numlist 2 3 4 `opb' {
		local i = `i' + 1
	xtreg 	`outcome'  	$instrument 	$linear  $covariets 	if inrange(runvar,-`b',`b')	, cluster(varselid) fe nonest
	local b`i'  = round(_b[$instrument] ,.001)
	local se`i' = round(_se[$instrument] ,.001)
	
	*Residulize year*moth fixed effects
	qui areg `outcome' $covariets if e(sample)==1, absorb(ym_def)
	predict e, residuals
	sum `outcome' 
	gen y_`i' = e + `r(mean)'
	drop e
	
	xtreg 	y_`i' 	$instrument 	$linear  	if e(sample)==1	& inrange(runvar,-`b',`b')	, cluster(varselid) fe nonest
	predict y_hat`i' if e(sample)==1,xb
	
	}
	
	keep if inrange(runvar,-4,4)
	
	gen temp =round(runvar*12)
	egen 	bin = cut(temp), at(-48(2)0)
	egen 	bin2 = cut(temp), at(1(2)49)
	replace bin = bin2 if bin==.
	replace bin = bin + .5 
	bys bin: egen outcome_bin = mean(y_3)
	 tab runvar bin
	
	
	collapse (mean) y_*  outcome_bin, by(runvar bin treat)
	
	replace runvar = runvar*12
	local opb = `opb'*12
	
	*Graph
	local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
	local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
	local Sstyle "lpattern(shortdash_dot) lcolor(gray) vertical" 
	local lineopt "lcolor(black) lpattern(dash)"
	twoway 	(scatter outcome_bin bin	 	 , ms(o) mcolor(gs10%10) mlcolor(gs10%10))  		///	
		(line 	 y_hat3 runvar  	if treat==0, lcolor(black) lpattern(longdash)) 	 		///
		(line 	 y_hat3 runvar  	if treat==1, lcolor(black) lpattern(longdash))	 		///
		///
		(line 	 y_hat2 runvar  	if treat==0, lcolor(navy) lpattern(solid)) 	 		///
		(line 	 y_hat2 runvar  	if treat==1, lcolor(navy) lpattern(solid))	 		///
		///
		(line 	 y_hat1 runvar  	if treat==0, lcolor(maroon) lpattern(dash_dot)) 	 	///
		(line 	 y_hat1 runvar  	if treat==1, lcolor(maroon) lpattern(dash_dot))	 		///
		///
		(line 	 y_hat4 runvar  	if treat==0, lcolor(teal) lpattern(shortdash)) 	 		///
		(line 	 y_hat4 runvar  	if treat==1, lcolor(teal) lpattern(shortdash))	 		///
		, xline(0 , `lineopt')   graphregion(color(white)) 					///
		  xscale(r(-48 48)) xlabel(-48(12)48) 	name(a,replace)					///
		  xtitle("Normalized age (months)") ytitle("Annual earnings (t+1) (1,000 SEK)") 	///
		  text(360 -48   "{&beta}{sup:48}=`b3'*** (`se3')"  ,place(right)  color(black))  ///
		  text(355 -48   "{&beta}{sup:36}=`b2'*** (`se2')"  ,place(right)  color(navy))  ///
		  text(350 -48   "{&beta}{sup:24}=`b1'*** (`se1')"  ,place(right)  color(maroon))  ///
		  text(345 -48   "{&beta}{sup:CCT}=`b4'*** (`se4')"  ,place(right)  color(teal)) ///
		  legend(order(2 "48" 4 "36" 6 "24" 8 "`opb' (CCT)") size(small) col(4) region(color(white)) subtitle("Bandwidth:", pos(9) size(small)))
	
	graph export "$agegraphpath/Robust/RD_`outcome'_t1.pdf", replace
restore		
}
*
{ // Non-employment for different bandwidths
*===============================================================================
preserve
	bys lopnr (date) : egen cumnonemployed 	= total(atfirm2==0 & working2==0) 	if inrange(eventtime_def,1,24) 
	keep if eventtime_def==1
	local outcome "cumnonemployed"
	*replace `outcome' = `outcome'/1000
	*==========
	xtset   ym_def
	*==========
	
	 rdbwselect `outcome' runvar   , vce(cluster varselid)
	local opb = round(e(h_mserd),.01)
	
	local i =0
	foreach b of numlist 2 3 4 `opb' {
		local i = `i' + 1
	xtreg 	`outcome'  	$instrument 	$linear  $covariets 	if inrange(runvar,-`b',`b')	, cluster(varselid) fe nonest
	local b`i'  = round(_b[$instrument] ,.001)
	local se`i' = round(_se[$instrument] ,.001)
	
	*Residulize year*moth fixed effects
	qui areg `outcome' $covariets if e(sample)==1, absorb(ym_def)
	predict e, residuals
	sum `outcome' 
	gen y_`i' = e + `r(mean)'
	drop e
	
	xtreg 	y_`i' 	$instrument 	$linear  	if e(sample)==1	& inrange(runvar,-`b',`b')	, cluster(varselid) fe nonest
	predict y_hat`i' if e(sample)==1,xb
	
	}
	
	keep if inrange(runvar,-4,4)
	
	gen temp =round(runvar*12)
	egen 	bin = cut(temp), at(-48(2)0)
	egen 	bin2 = cut(temp), at(1(2)49)
	replace bin = bin2 if bin==.
	replace bin = bin + .5 
	bys bin: egen outcome_bin = mean(y_3)
	 tab runvar bin
	
	
	collapse (mean) y_*  outcome_bin, by(runvar bin treat)
	
	replace runvar = runvar*12
	local opb = `opb'*12
	
	*Graph
	local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
	local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
	local Sstyle "lpattern(shortdash_dot) lcolor(gray) vertical" 
	local lineopt "lcolor(black) lpattern(dash)"
	twoway 	(scatter outcome_bin bin	 	 , ms(o) mcolor(gs10%10) mlcolor(gs10%10))  		///	
		(line 	 y_hat3 runvar  	if treat==0, lcolor(black) lpattern(longdash)) 	 		///
		(line 	 y_hat3 runvar  	if treat==1, lcolor(black) lpattern(longdash))	 		///
		///
		(line 	 y_hat2 runvar  	if treat==0, lcolor(navy) lpattern(solid)) 	 		///
		(line 	 y_hat2 runvar  	if treat==1, lcolor(navy) lpattern(solid))	 		///
		///
		(line 	 y_hat1 runvar  	if treat==0, lcolor(maroon) lpattern(dash_dot)) 	 	///
		(line 	 y_hat1 runvar  	if treat==1, lcolor(maroon) lpattern(dash_dot))	 		///
		///
		(line 	 y_hat4 runvar  	if treat==0, lcolor(teal) lpattern(shortdash)) 	 		///
		(line 	 y_hat4 runvar  	if treat==1, lcolor(teal) lpattern(shortdash))	 		///
		, xline(0 , `lineopt')   graphregion(color(white)) 					///
		  xscale(r(-48 48)) xlabel(-48(12)48) 	name(b,replace)					///
		  xtitle("Normalized age (months)") ytitle("Cumulated months in non-employment (within 2 years)", size(small)) 	///
		  text(9 -48   "{&beta}{sup:48}=`b3'*** (0`se3')"  ,place(right)  color(black))  ///
		  text(8.8 -48   "{&beta}{sup:36}=`b2'*** (0`se2')"  ,place(right)  color(navy))  ///
		  text(8.6 -48   "{&beta}{sup:24}=`b1'*** (0`se1')"  ,place(right)  color(maroon))  ///
		  text(8.4 -48   "{&beta}{sup:CCT}=`b4'*** (0`se4')"  ,place(right)  color(teal)) ///
		  legend(order(2 "48" 4 "36" 6 "24" 8 "`opb' (CCT)") size(small) col(4) region(color(white)) subtitle("Bandwidth:", pos(9) size(small)))
	
	graph export "$agegraphpath/Robust/RD_`outcome'_t1.pdf", replace
restore		
}
*


log close
